package cn.edu.pzhu.javaweb.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.pzhu.javaweb.pojo.Result;
import cn.edu.pzhu.javaweb.pojo.User;
import cn.edu.pzhu.javaweb.service.UserService;
import cn.edu.pzhu.javaweb.service.imp.UserServiceImp;

/**
 * 修改密码控制器
 */
@WebServlet("/changepwd")
public class ChangepwdServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private UserService userService = new UserServiceImp();
    
    /**
     * 构造函数
     */
    public ChangepwdServlet() {
        super();
    }

    /**
     * 处理GET请求
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        
        // 获取session对象
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        
        // 转发到修改密码页面
        request.getRequestDispatcher("/WEB-INF/changepwd.jsp").forward(request, response);
    }

    /**
     * 处理POST请求 - 执行密码修改逻辑
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        
        // 获取session对象
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        // 检查用户是否登录
        if (username == null || username.isEmpty()) {
            response.getWriter().print("<script>alert('请先登录！');location.href='login.jsp';</script>");
            return;
        }
        // 获取表单提交的参数
        String oldPassword = request.getParameter("pwd");
        String newPassword = request.getParameter("npwd");
        String confirmPassword = request.getParameter("rpwd");
        // 数据验证
        if (oldPassword == null || oldPassword.isEmpty() || newPassword == null || newPassword.isEmpty() 
                || confirmPassword == null || confirmPassword.isEmpty()) {
            response.getWriter().print("<script>alert('密码信息不完整！');location.href='changepwd';</script>");
            return;
        }
        // 检查两次输入的新密码是否一致
        if (!newPassword.equals(confirmPassword)) {
            response.getWriter().print("<script>alert('两次输入的新密码不一致！');location.href='changepwd';</script>");
            return;
        }
        
        // 使用Service层进行密码修改
        Result<String> result = userService.UpdatePassword(username, oldPassword, newPassword);
        if (result.getCode() == 0) {
            // 销毁当前session，确保用户必须重新登录
            session.invalidate();
            // 提示成功并跳转到登录页面
            response.getWriter().print("<script>alert('密码修改成功，请用新密码重新登录！');location.href='login.jsp';</script>");
        } else {
            // 提示失败信息
            response.getWriter().print("<script>alert('" + result.getMessage() + "');location.href='changepwd';</script>");
        }
    }

}